package com.retail.bbq.cola.enums;

import org.springframework.util.AntPathMatcher;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public enum ApiForwardRedisEnum {
    LOTTERY_CACHE_INFO("抽奖活动缓存信息", "/api/icoke-lottery/{buCode}/lottery/h5/open/{setupId}/detail","lotteryInfoCache:%s"),
    ORDER_CACHE_INFO("订单缓存信息", "/api/icoke-lottery/{buCode}/lottery/h5/open/{setupId}/readRecords","orderInfoCache:%s:%s"),
    ;
    private String name;
    private String path;
    private String apiRedisKey;

    ApiForwardRedisEnum(String name, String path, String apiRedisKey) {
        this.name = name;
        this.path = path;
        this.apiRedisKey = apiRedisKey;

    }

    public String getName() {
        return this.name;
    }

    public String getPath() {
        return this.path;
    }

    public String getApiRedisKey() {
        return this.apiRedisKey;
    }

    private static final Map<String, ApiForwardRedisEnum> pathMap = new HashMap<>();

    static {
        Arrays.stream(ApiForwardRedisEnum.values())
                .forEach(val -> {
                    pathMap.put(val.getPath(), val);
                });
    }

    public static ApiForwardRedisEnum getEnumByPath(String path) {
        return pathMap.get(path);
    }

    public static ApiForwardRedisEnum getMatchPathEnumByPath(String urlPath) {
        AntPathMatcher matcher = new AntPathMatcher();
        Set<String> paths = pathMap.keySet();
        for(String path : paths){
            if(matcher.match(path, urlPath)){
                return pathMap.get(path);
            }
        }
        return null;
    }
}
